﻿using LD.BC;
using LD.Host;
using LD.Host.Tool;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.Tool
{
    public partial class ToolScanerFrm : FrmToolBaseSkip//FrmToolBase
    {
        public ToolScanerFrm()
        {
            InitializeComponent();
            this.Shown += ToolMoveFrm_Shown;
            this.btn_ok.Click += Btn_ok_Click;
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            List<ScanerModel> scaners = new List<ScanerModel>();
            foreach (DataGridViewRow item in this.dgv_data.Rows)
            {
                if (item.Cells["扫码枪"].Value != null &&
                    item.Cells["传出值"].Value != null &&
                    item.Cells["别名"].Value != null &&
                    int.TryParse(item.Cells["超时时间ms"].Value.ToString(), out var timeDate))
                {
                    scaners.Add(new ScanerModel()
                    {
                        Alias = item.Cells["别名"].Value.ToString(),
                        Name = item.Cells["扫码枪"].Value.ToString(),
                        TimeOut = timeDate,
                        Value = item.Cells["传出值"].Value.ToString()
                    });
                    var param = new ScanerParam()
                    {
                        Scaners = scaners,
                    };

                    this.TB_Tool.Param = JsonConvert.SerializeObject(param);
                }
            }
        }

        private void ToolMoveFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }
        private DataTable GetVar()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < Cache.Var.Count; i++)
            {
                dt.Rows.Add(i, Cache.Var[i].Name);
            }
            return dt;
        }
        private DataTable GetScaner()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            int index = 0;
            foreach (var item in CommunicateFactory.Instance.TcpIp)
            {
                dt.Rows.Add(index, item.Value.Name);
                index++;
            }
            return dt;
        }
        private void Init()
        {
            DataTable scaner = GetScaner();
            this.扫码枪.DataSource = scaner;
            this.扫码枪.ValueMember = "name";
            this.扫码枪.DisplayMember = "name";

            DataTable var = GetVar();
            this.传出值.DataSource = var;
            this.传出值.ValueMember = "name";
            this.传出值.DisplayMember = "name";

            if (this.TB_Tool.Param == null)
                return;
            var param = JsonConvert.DeserializeObject<ScanerParam>(this.TB_Tool.Param);
            if (param != null)
            {
                foreach (var item in param.Scaners)
                {
                    int index = this.dgv_data.Rows.Add();
                    this.dgv_data["别名", index].Value = item.Alias;
                    this.dgv_data["超时时间ms", index].Value = item.TimeOut;
                    DataGridViewComboBoxCell cobCellName1 = this.dgv_data["扫码枪", index] as DataGridViewComboBoxCell;
                    cobCellName1.Value = item.Name;

                    DataGridViewComboBoxCell cobCellName2 = this.dgv_data["传出值", index] as DataGridViewComboBoxCell;
                    cobCellName2.Value = item.Value;
                }
            }
        }
        private void tsb_new_Click(object sender, EventArgs e)
        {
            this.dgv_data.Rows.Add();
        }

        private void dgv_data_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            if (e.ColumnIndex == 0 && this.dgv_data[e.ColumnIndex, 0].Value != null)
            {
                if (int.TryParse(this.dgv_data[e.ColumnIndex, 0].Value.ToString(), out var iotype))
                //&& int.TryParse(this.dgv_data[e.ColumnIndex, 1].Value.ToString(), out var iono))
                {
                    var io = BcFactory.Instance.Config.IoList.Where(p => p.IoType == iotype).ToList();
                    if (io.Any())
                    {
                        DataGridViewComboBoxCell cobCellName1 = this.dgv_data["IO名称", e.RowIndex] as DataGridViewComboBoxCell;
                        cobCellName1.DataSource = io;
                        cobCellName1.DisplayMember = "Name";
                        cobCellName1.ValueMember = "No";
                    }
                    //this.dgv_data[e.ColumnIndex, 1].Value = io.Name;
                }
            }
        }

        private void tsb_sub_Click(object sender, EventArgs e)
        {
            if (this.dgv_data.SelectedRows.Count > 0)
            {
                var rows = this.dgv_data.SelectedRows;
                foreach (DataGridViewRow item in rows)
                {
                    this.dgv_data.Rows.Remove(item);
                }
            }
        }
    }
}
